#Table 2

#directory
#set working directory to the path PvP_Replication
#setwd("~/PvP_Replication")

#libraries
library(texreg)
library(tidyverse)
library(estimatr)

#read in main dataset
pvp_data <- read.csv("PvP_data/PvP_data_main.csv")

#transform coca cultivation variable
#create  log + 1 cultivation variable
pvp_data <- pvp_data %>% 
  mutate(logcult = log(maxcult + 1))

#subset main dataset to municipalities with prior FARC presence
pvp_farc_subset <- pvp_data %>% filter(farc_presence == 1)

#transform cocaine paste seizure variable
pvp_farc_subset <- pvp_farc_subset %>% 
  mutate(log_paste_kg = log(paste_kg_tot + 1)) #take log

# estimate models
ols_traffic_no_controls <- lm_robust(dissident_presence ~ traffic_hub_ct, pvp_farc_subset)
ols_traffic <- lm_robust(dissident_presence ~ traffic_hub_ct + logcult, pvp_farc_subset)
ols_placebo <- lm_robust(dissident_presence ~ placebo_hub_ct + logcult, pvp_farc_subset)
ols_paste_out <- lm_robust(log_paste_kg ~ traffic_hub_ct + logcult, pvp_farc_subset)

#generate table
texreg(list(
  ols_traffic_no_controls, ols_traffic, ols_placebo, ols_paste_out),
  digits = 2, include.ci = FALSE, single.row = FALSE, include.fstatistic = TRUE, 
  include.rmse = FALSE, include.rsquared = FALSE, include.adjrs = FALSE, 
  include.nobs = TRUE, stars = c(0.001, 0.01, 0.05), float.pos = "h", caption.above	= TRUE, 
  caption = "Drug Trafficking Hubs and FARC Splinter Group Emergence", 
  custom.coef.map	= list("(Intercept)" = "(Intercept)", "traffic_hub_ct" = "Predicted Trafficking Hubs (count)", "placebo_hub_ct" = "Placebo Hubs (count)", "logcult" = "Coca Cultivation (log ha.)"),
  custom.header = list("DV:Splinter Group Presence (binary)" = 1:3, "DV: Coca Paste Seizures (log kg.)" = 4)
)
